package br.ufsc.ctc.ine5622.compilador.gui;

import br.ufsc.ctc.ine5622.compilador.arquivo.Extensao;

import java.io.File;

import javax.swing.filechooser.FileFilter;

/**
 * This class filters files for the fileChooser in the GUI, outputting only the
 * ones with valid extensions.
 * 
 * @author William di Domenico
 * @author Paulo Pinho
 * @see FileFilter
 */
public class CustomFileFilter extends FileFilter {

	/**
	 * Check if a file has a valid extension by consulting
	 * the enum that holds them
	 * @param file The file to check for
	 * @return true if the file is valid, false if not
	 * @see Extensao
	 */
	@Override
	public boolean accept(File file) {
		Extensao[] validExtensions = Extensao.values();

		if (file.isDirectory()) {
			return true;
		}

		for (Extensao validExtension : validExtensions) {
			if (file.getName().endsWith(
					"." + validExtension.toString().toLowerCase())) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Returns the text that will show up in the interface dropdown
	 * for choosing the extensions
	 * @return Extensions that will be shown
	 * @see FileFilter
	 * @see JanelaPrincipal
	 */
	@Override	
	public String getDescription() {
		return "*.lsi, *.txt";
	}
}
